package com.estate.struts.actions;

import java.util.HashMap;
import java.util.List;

import com.estate.utils.CertificateCatagoryUtil;
import com.estate.utils.DocumentStateUtil;
import com.estate.utils.LendingAuthorStateUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.LibraryVo;
import com.estate.vo.impl.DocumentVo;
import com.estate.vo.impl.PriceProjectVo;
import com.estate.business.AllProjectService;
import com.estate.business.DocumentService;
import com.estate.business.LendDocumentService;
import com.estate.business.LibraryService;
import com.estate.business.PriceProjectService;

public class DocumentAction extends EstateSupport {
	
	private int id;
	private String documentNum;
	private String documentName;
	private LibraryVo library;
	private String fondNum;
	private int catagory;
	private int secretRate;
	private int state;
	private int proId;
	private int proCatagory;
	private int lendState;
	private String proName;
	private int LibraryId;
	private String catagoryName;
	
	private DocumentService documentService;
	private PriceProjectService priceProjectService;
	private LibraryService libraryService;
	private LendDocumentService lendDocumentService;
	private AllProjectService allProjectService;
	
	private String method;
	private String checkId;
	private String modifyId;
	private String multiType;
	private String deleteId;
	private String addfileId;
	private String changeId;
	private String updateId;
	private String multiDelete;
	private String delStr;
	
	public String execute() throws Exception{
		System.out.println(method);
		String page="";
		if("list".equals(method)){
			List<DocumentVo> voList=documentService.findAllDocument();
			for(int i=0;i<voList.size();i++){
				
				voList.get(i).setStateName(DocumentStateUtil.getStateString(voList.get(i).getState()));
				voList.get(i).setProName(getProNameByIC(voList.get(i).getProId(),voList.get(i).getProCatagory()));
				System.out.println(voList.get(i).getProName()+"@@@@@@@");
				voList.get(i).setCatagoryName(CertificateCatagoryUtil.getCatagoryString(voList.get(i).getCatagory()));
				voList.get(i).setLendStateName(LendingAuthorStateUtil.getAuthorStateString(voList.get(i).getLendState()));
			}
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			this.getSession().setAttribute("documentList", voList);
			System.out.println("aa");
			page="list";
		} else if("lending".equals(method)){
			List<DocumentVo> voList=documentService.findByLendState(1);
			voList.addAll(documentService.findByLendState(3));
			voList.addAll(documentService.findByLendState(4));
			for(int i=0;i<voList.size();i++){
				voList.get(i).setLendStateName(LendingAuthorStateUtil.getAuthorStateString(voList.get(i).getLendState()));
				voList.get(i).setStateName(DocumentStateUtil.getStateString(voList.get(i).getState()));
				voList.get(i).setProName(getProNameByIC(voList.get(i).getProId(),voList.get(i).getProCatagory()));
				voList.get(i).setCatagoryName(CertificateCatagoryUtil.getCatagoryString(catagory));
			}
			this.getSession().setAttribute("documentList", voList);
			page="lending";
		} else if("add".equals(method)){
			System.out.println("aa");
			HashMap<Integer,String> libraryMap=libraryService.findAllIdAndName();
			this.getSession().setAttribute("librarySelect", libraryMap);
			System.out.println("aaa");
			HashMap<Integer,String> catagoryMap=new HashMap<Integer,String>();
			catagoryMap.put(1, CertificateCatagoryUtil.getCatagoryString(1));
			catagoryMap.put(2, CertificateCatagoryUtil.getCatagoryString(2));
			System.out.println(catagoryMap);
			this.getSession().setAttribute("catagorySelect", catagoryMap);
			page="add";
		} else if("insert".equals(method)){
			DocumentVo vo=this.getNewDocument();
			documentService.addDocument(vo);
			page="refresh";
		} else if("delete".equals(method)){
			if ("true".equals(multiDelete)) {
				System.out.println("!!!!!!!!!"+delStr);
				if (delStr != null && !"".equals(delStr) && !",".equals(delStr)) {
					String[] delArray = delStr.split(",");
					for (int i = 0; i < delArray.length; i++) {
						documentService.deleteByPK(Integer
								.parseInt(delArray[i]));
					}
				}
			} else {
				documentService.deleteByPK(Integer.parseInt(this.getDeleteId()));
			}
			
			page="refresh";
		} else if("addfile".equals(method)){
			DocumentVo vo=(DocumentVo)documentService.findByPK(Integer.parseInt(this.getAddfileId()));
			this.getSession().setAttribute("documentName", vo.getDocumentName());
			this.getSession().setAttribute("documentNum", vo.getDocumentNum());
			this.getSession().setAttribute("documentId", vo.getId());
			page="addfile";
		} else if("change".equals(method)){
			DocumentVo vo=(DocumentVo)documentService.findByPK(Integer.parseInt(this.getChangeId()));
			if(vo.getState()==1){
				vo.setState(2);
			} else if(vo.getState()==2){
				vo.setState(1);
			}
			documentService.update(vo);
			page="refresh";
		} else if("modify".equals(method)){
			DocumentVo vo=(DocumentVo)documentService.findByPK(Integer.parseInt(this.getModifyId()));
			vo.setCatagoryName(CertificateCatagoryUtil.getCatagoryString(vo.getCatagory()));
			HashMap<Integer,String> catagoryMap=new HashMap<Integer,String>();
			catagoryMap.put(1, CertificateCatagoryUtil.getCatagoryString(1));
			catagoryMap.put(2, CertificateCatagoryUtil.getCatagoryString(2));
			this.getSession().setAttribute("modifyList", vo);
			this.getSession().setAttribute("library", vo.getLibrary());
			HashMap<Integer,String> libraryMap=libraryService.findAllIdAndName();
			this.getSession().setAttribute("librarySelect", libraryMap);
			page="modify";
		} else if("update".equals(method)){
			DocumentVo vo=this.getModifyDocument(Integer.parseInt(this.getUpdateId()));
			documentService.update(vo);
			page="list";
		} else if("statistics".equals(method)){
			List<DocumentVo> voList=documentService.findAllDocument();
			for(int i=0;i<voList.size();i++){
				voList.get(i).setLendStateName(LendingAuthorStateUtil.getAuthorStateString(voList.get(i).getLendState()));
				voList.get(i).setStateName(DocumentStateUtil.getStateString(voList.get(i).getState()));
				voList.get(i).setProName(getProNameByIC(voList.get(i).getProId(),voList.get(i).getProCatagory()));
				voList.get(i).setCatagoryName(CertificateCatagoryUtil.getCatagoryString(voList.get(i).getCatagory()));
				voList.get(i).setTimes(lendDocumentService.getTimeofDocument(voList.get(i).getId()));
			}
			this.getSession().setAttribute("documentList", voList);
			page="statistics";
		} else if("docsta".equals(method)){
			List<DocumentVo> voList=documentService.findAllDocument();
			for(int i=0;i<voList.size();i++){
				voList.get(i).setFileNum(lendDocumentService.getTimeofDocument(voList.get(i).getId()));
				voList.get(i).setStateName(DocumentStateUtil.getStateString(voList.get(i).getState()));
				voList.get(i).setCatagoryName(CertificateCatagoryUtil.getCatagoryString(voList.get(i).getCatagory()));
			}
			this.getSession().setAttribute("documentList", voList);
			page="docsta";
		}
		return page;
	}
	
	public DocumentVo getNewDocument(){
		DocumentVo vo=new DocumentVo();
		vo.setCatagory(catagory);
		vo.setDocumentName(documentName);
		vo.setDocumentNum(documentNum);
		vo.setLibrary((LibraryVo)libraryService.findByPK(LibraryId));
		vo.setSecretRate(secretRate);
		vo.setFondNum(fondNum);
		vo.setProCatagory(proCatagory);
		System.out.println("11");
		vo.setProId(proId);
		System.out.println("22");
		if("1".equals(proCatagory)){
			vo.setProName(((PriceProjectVo)allProjectService.findProjectByCAndId(1, proId)).getName());
		} else if("2".equals(proCatagory)){
			
		} else if("3".equals(proCatagory)){
			
		}
		vo.setState(1);
		vo.setLendState(1);
		return vo;
	}
	
	public DocumentVo getModifyDocument(int id){
		DocumentVo vo=(DocumentVo)documentService.findByPK(id);
		vo.setCatagory(catagory);
		vo.setDocumentName(documentName);
		vo.setDocumentNum(documentNum);
		vo.setLibrary((LibraryVo)libraryService.findByPK(LibraryId));
		vo.setSecretRate(secretRate);
		vo.setFondNum(fondNum);
		return vo;
	}
	
	public String getProNameByIC(int id,int catagory){
		String proName="";
		switch(catagory){
		case 1:
			PriceProjectVo priceProjectVo=(PriceProjectVo)priceProjectService.findByPK(id);
			proName=priceProjectVo.getName();
			break;
		case 2:
			break;
		case 3:
			break;
			
		}
		return proName;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getDocumentNum() {
		return documentNum;
	}
	public void setDocumentNum(String documentNum) {
		this.documentNum = documentNum;
	}
	
	public LibraryVo getLibrary() {
		return library;
	}
	public void setLibrary(LibraryVo library) {
		this.library = library;
	}
	public String getFondNum() {
		return fondNum;
	}
	public void setFondNum(String fondNum) {
		this.fondNum = fondNum;
	}
	public int getCatagory() {
		return catagory;
	}
	public void setCatagory(int catagory) {
		this.catagory = catagory;
	}
	public int getSecretRate() {
		return secretRate;
	}
	public void setSecretRate(int secretRate) {
		this.secretRate = secretRate;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public int getProId() {
		return proId;
	}
	public void setProId(int proId) {
		this.proId = proId;
	}
	public int getProCatagory() {
		return proCatagory;
	}
	public void setProCatagory(int proCatagory) {
		this.proCatagory = proCatagory;
	}
	public int getLendState() {
		return lendState;
	}
	public void setLendState(int lendState) {
		this.lendState = lendState;
	}
	public void setDocumentName(String documentName) {
		this.documentName = documentName;
	}
	public String getDocumentName() {
		return documentName;
	}
	public void setProName(String proName) {
		this.proName = proName;
	}
	public String getProName() {
		return proName;
	}
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}
	public DocumentService getDocumentService() {
		return documentService;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMethod() {
		return method;
	}
	public void setCheckId(String checkId) {
		this.checkId = checkId;
	}
	public String getCheckId() {
		return checkId;
	}
	public void setModifyId(String modifyId) {
		this.modifyId = modifyId;
	}
	public String getModifyId() {
		return modifyId;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setPriceProjectService(PriceProjectService priceProjectService) {
		this.priceProjectService = priceProjectService;
	}

	public PriceProjectService getPriceProjectService() {
		return priceProjectService;
	}

	public void setLibraryService(LibraryService libraryService) {
		this.libraryService = libraryService;
	}

	public LibraryService getLibraryService() {
		return libraryService;
	}

	public void setLibraryId(int libraryId) {
		LibraryId = libraryId;
	}

	public int getLibraryId() {
		return LibraryId;
	}

	public void setCatagoryName(String catagoryName) {
		this.catagoryName = catagoryName;
	}

	public String getCatagoryName() {
		return catagoryName;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setAddfileId(String addfileId) {
		this.addfileId = addfileId;
	}

	public String getAddfileId() {
		return addfileId;
	}

	public void setChangeId(String changeId) {
		this.changeId = changeId;
	}

	public String getChangeId() {
		return changeId;
	}

	public void setUpdateId(String updateId) {
		this.updateId = updateId;
	}

	public String getUpdateId() {
		return updateId;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setLendDocumentService(LendDocumentService lendDocumentService) {
		this.lendDocumentService = lendDocumentService;
	}

	public LendDocumentService getLendDocumentService() {
		return lendDocumentService;
	}

	public void setAllprojectService(AllProjectService allProjectService) {
		this.allProjectService = allProjectService;
	}

	public AllProjectService getAllProjectService() {
		return allProjectService;
	}

}
